package com.itheima.cs.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.widget.Toast;

import androidx.annotation.RequiresApi;


public class CommonUtils {

    public static  void showShortMsg(Context context,String msg){
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }


    public static  void showLongMsg(Context context,String msg){
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

    public static  void showDlgMsg(Context context,String msg){
        new AlertDialog.Builder(context)
                .setTitle("提示消息")
                .setMessage(msg)
                .setPositiveButton("确定",null)
                .setNegativeButton("取消",null)
                .create().show();
    }

    public static boolean checkWifiInfo(Context context) {
        String wifiName = getWifiName(context);
        if (wifiName.startsWith("TSZG")) {
            return true;
        } else {
            return false;
        }
    }

    @RequiresApi(api = android.os.Build.VERSION_CODES.LOLLIPOP)
    public static String getWifiName(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkCapabilities capabilities = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
            }
            if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                if (wifiManager != null && wifiManager.isWifiEnabled()) {
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    if (wifiInfo != null) {
                        String ssid = wifiInfo.getSSID();
                        if (ssid != null) {
                            return ssid.replace("\"", ""); // 去除SSID中的引号
                        }
                    }
                }
            }
        }
        return null;
    }

}

